<?php

namespace addons\wanlshop\job;


use app\admin\model\User;
use app\admin\model\wanlshop\Order;
use app\api\model\wanlshop\CouponReceive;
use think\Model;
use think\queue\Job;


/**
 * 订单自动操作
 */
class OrderAutoOper extends BaseJob
{

    /**
     * 订单自动关闭
     */
    public function autoClose(Job $job, $data){

        try {
            $order = $data['order'];
            // 重新查询订单
            $order = Order::get($order['order_id']);
            if ($order && $order->state == '1') {
                \think\Db::transaction(function () use ($order, $data) {
                    // 执行关闭
                    $order->state = Order::STATUS_INVALID;
                    $order->auto_time = time();      // 取消时间
                    $order->save();
                    // 订单自动关闭之后 行为 返还用户优惠券，积分
                    if($order->coupon_id != 0){
                        model('app\api\model\wanlshop\CouponReceive')->where(['id' => $order->coupon_id, 'user_id' => $order->user_id])->update(['state' => 1]);
                    }
                    // 释放库存 1.0.3升级
                    if($order->type == 'shop'){
                        foreach(model('app\api\model\wanlshop\OrderGoods')->all(['order_id' => $order->id]) as $vo){
                            // 查询商品是否需要释放库存
                            if(model('app\api\model\wanlshop\Goods')->get($vo['goods_id'])['stock'] == 'porder'){
                                model('app\api\model\wanlshop\GoodsSku')->where('id', $vo['goods_sku_id'])->setInc('stock', $vo['number']);
                            }
                        }
                    }
                });
            }

            // 删除 job
            $job->delete();
        } catch (\Exception $e) {
            // 队列执行失败
            \think\Log::write('queue-' . get_class() . '-autoClose' . '：执行失败，错误信息：' . $e->getMessage());
        }
    }



    /**
     * 优惠劵自动关闭
     */
    public function couponClose(Job $job, $data){

        try {
            // 重新查询优惠卷
            $coupon = CouponReceive::get($data['id']);
            if ($coupon && $coupon->state == '1') {
                \think\Db::transaction(function () use ($coupon, $data) {
                    // 执行关闭
                    $coupon->state = 3;
                    $coupon->save();
                });
            }
            // 删除 job
            $job->delete();
        } catch (\Exception $e) {
            // 队列执行失败
            \think\Log::write('queue-' . get_class() . '-couponClose' . '：执行失败，错误信息：' . $e->getMessage());
        }
    }


    /**
     * 会员过期自动变回 默认 1级会员关闭
     */
    public function memberClose(Job $job, $data){

        try {
            // 重新查询用户
            $info = User::get($data['id']);
            if ($info && $info->level > '1') {
                \think\Db::transaction(function () use ($info, $data) {
                    // 执行关闭
                    $info->level = 3;
                    $info->member_expire_time = -1;
                    $info->save();
                });
            }
            // 删除 job
            $job->delete();
        } catch (\Exception $e) {
            // 队列执行失败
            \think\Log::write('queue-' . get_class() . '-memberClose' . '：执行失败，错误信息：' . $e->getMessage());
        }
    }


}